package com.rd.student.jike.class2.lihongming.StudentSystem;


import java.util.*;

/**
 * @Auther:Heinrich
 * @Date:2023/7/12-07-12-12:32
 * @Description:com.rd.student.jike.class2.lihongming.StudentSystem
 * @version:1.0
 */
public class SystemMethod {
    static Scanner scanner = new Scanner(System.in);

    /**
     * 展示全部学生信息
     * @param studentG4s 储存学生信息的链表
     */
    public static void showStudent(LinkedList<StudentG4> studentG4s) {
        ListIterator listIterator = studentG4s.listIterator();
        while (listIterator.hasNext()) {
            Object o = listIterator.next();
            System.out.println(o);
        }
    }

    /**
     * 向学生链表中添加新的学生信息
     * @param students 储存学生信息的学生链表
     */
    public static void addStudent(LinkedList students) {
        StudentG4 s = new StudentG4();
        System.out.println("请输入学生姓名");
        String answerName = scanner.next();
        if (checkRepetition(students, answerName)) {
            s.setName(answerName);
        } else {
            System.out.println("该学生已存在，请检查是否重复录入，插入失败");
            return;
        }
        System.out.println("请输入学生班级");
        String answerCla = scanner.next();
        s.setCls(answerCla);

        System.out.println("请输入学生专业");
        String answerDept = scanner.next();
        s.setMajor(answerDept);

        System.out.println("请输入学生描述");
        String answerComment = scanner.next();
        s.setDesc(answerComment);

        System.out.println("请输入学生年龄");
        Integer answerAge = scanner.nextInt();
        s.setAge(answerAge);

        if (s.getName() == null || s.getCls() == null || s.getMajor() == null || s.getAge() == null) {
            System.out.println("录入信息不规范，插入失败");
            StudentG4.ORDER--;
        } else {
            System.out.println("插入成功！");
            students.add(s);
            SystemMethod.showStudent(students);
        }
    }

    /***
     * 删除成员
     * @param studentG4s 储存学生信息的学生链表
     * @param name
     */
    public static void deleteStudent(LinkedList<StudentG4> studentG4s, String name) {
        if (studentG4s.size() <= 1) {
            System.out.println("列表只有一个成员，删除失败");
            return;
        }
        for (int i = 0; i < studentG4s.size(); i++) {
            if (studentG4s.get(i).getName().equals(name)||String.valueOf(studentG4s.get(i).getID()).equals(name)) {
                studentG4s.remove(i);
                return;
            }
        }
        System.out.println("该成员不存在，删除失败");
    }

//    public static void deleteStudent(LinkedList<Student> students, int ID) {
//        if (students.size() <= 1) {
//            System.out.println("列表只有一个成员，删除失败");
//            return;
//        }
//        for (int i = 0; i < students.size(); i++) {
//            if (students.get(i).getID() == ID) {
//                students.remove(i);
//                return;
//            }
//        }
//        System.out.println("该成员不存在，删除失败");
//    }


    /**
     * 校验重复
     *
     * @param studentG4s 被校验的学生链表
     * @param name     要校验的姓名
     * @return 是否重复
     */
    private static boolean checkRepetition(LinkedList<StudentG4> studentG4s, String name) {
        ListIterator listIterator = studentG4s.listIterator();
        while (listIterator.hasNext()) {
            StudentG4 s = (StudentG4) listIterator.next();
            if (s.getName().equals(name)) {
                return false;
            }
        }
        return true;
    }

    //功能3 薛志鑫

    /***
     * @Auther:薛志鑫
     * 根据班级分组
     * @param studentG4s 储存学生信息的学生链表
     */
    public static void inquireCls(LinkedList<StudentG4> studentG4s) {
        Scanner scanner = new Scanner(System.in);
        HashMap hashMap = new HashMap();
        for (int i = 0; i < studentG4s.size(); i++) {
            StudentG4 studentG4 = studentG4s.get(i);
            if (hashMap.containsKey(studentG4.getCls())) {
                int num = (int) hashMap.get(studentG4.getCls()) + 1;
                hashMap.put(studentG4.getCls(), num);
            } else {
                hashMap.put(studentG4.getCls(), 1);
            }
        }

        Set set = hashMap.keySet();
        Iterator iterator = set.iterator();
        int a = 0;
        System.out.println("输入序号查看班级成员具体信息");
        while (iterator.hasNext()) {
            a++;
            Object key = iterator.next();
            Object value = hashMap.get(key);
            System.out.println(a + ". 班级为：" + key + " 人数为：" + value);
        }

        ArrayList list1 = new ArrayList<>();
        for (int i = 0; i < hashMap.size(); i++) {
            for (Object cls : hashMap.keySet()) {
                list1.add(cls);
            }
        }

        int numCls = scanner.nextInt();
        if (numCls > list1.size()) {
            System.out.println("您输入的选项出差");
        } else {
            String cls = (String) list1.get(numCls - 1);
            for (int i = 0; i < studentG4s.size(); i++) {
                StudentG4 s2 = studentG4s.get(i);
                if (cls.equals(s2.getCls())) {
                    System.out.println(studentG4s.get(i));
                }
            }
        }
    }


    //功能4 夏银银

    /***
     * 根据专业分组
     * @Auther:夏银银
     * @param students 储存学生信息的链表
     * @return
     */
    public static HashMap MajorInquir(LinkedList students) {
        HashMap hashMap = new HashMap();
        for (int i = 0; i < students.size(); i++) {
            StudentG4 studentG4 = (StudentG4) students.get(i);
            String key = studentG4.getMajor();
            if (!hashMap.containsKey(key)) {
                hashMap.put(key, 1);
            } else {
                int value = (int) hashMap.get(key);
                hashMap.put(key, value + 1);
            }
        }
        System.out.println("可以输入专业查询成员列表");
        Scanner sc = new Scanner(System.in);
        String major = sc.nextLine();
        LinkedList list1 = new LinkedList();
        for (int i = 0; i < students.size(); i++) {
            StudentG4 s = (StudentG4) (students.get(i));
            if (s.getMajor().equals(major)) {
                list1.add(students.get(i));
            }
        }

        for (int i = 0; i < list1.size(); i++) {
            System.out.println(list1.get(i));
        }
        return hashMap;
    }

    //功能5 王海鑫

    /***
     * 根据年龄分组
     * @Auther:王海鑫
     * @param students 储存学生信息的链表
     * @return
     */
    public static HashMap ageQuery(LinkedList students) {
        int count = 1;
        HashMap hashMap = new HashMap();
        for (int i = 0; i < students.size(); i++) {
            StudentG4 s = (StudentG4) students.get(i);
            if (!hashMap.containsKey(s.getAge())) {
                count = 1;
                hashMap.put(s.getAge(), count);
            } else {
                hashMap.put(s.getAge(), ++count);
            }
        }

        return hashMap;
    }

    /***
     * 查看具体年龄的所有成员
     * @Auther:王海鑫
     * @param students 储存学生信息的链表
     * @param age 要查询的年龄
     * @return
     */
    public static LinkedList ageMember(LinkedList students, int age) {
        LinkedList linkedList = new LinkedList();
        for (int i = 0; i < students.size(); i++) {
            StudentG4 s = (StudentG4) students.get(i);
            if (s.getAge() == age) {
                linkedList.add(s.getName());
            }
        }
        return linkedList;
    }


    //功能6 王向阳

    /***
     * 整合所有成员名字
     * @Auther:王向阳
     * @param studentG4s 储存学生信息的链表
     */
    public static void getAllNames(LinkedList<StudentG4> studentG4s) {
        System.out.println("所有成员的名字：");
        for (StudentG4 member : studentG4s) {
            System.out.println(member.getName());
        }
    }

    //功能7 李宏鸣

    /**
     * 平均年龄
     * @Auther:李宏鸣
     * @param studentG4s 学生信息储存列表
     */
    public static void averageAge(LinkedList<StudentG4> studentG4s) {
        ListIterator listIterator = studentG4s.listIterator();
        int averageAge = 0;
        while (listIterator.hasNext()) {
            StudentG4 s = (StudentG4) listIterator.next();
            averageAge += s.getAge();
        }
        averageAge /= studentG4s.size();
        System.out.println("平均年龄是" + averageAge);
        System.out.println("------从小到大排序------");
        Collections.sort(studentG4s);
        for (int i = 0; i < studentG4s.size(); i++) {
            System.out.println(studentG4s.get(i));
        }
        System.out.println("------从大到小排序------");
        Collections.reverse(studentG4s);
        for (int i = 0; i < studentG4s.size(); i++) {
            System.out.println(studentG4s.get(i));
        }

    }

    //功能8 吴俊豪

    /***
     * 根据年龄范围返回成员列表
     * @Auther:吴俊豪
     * @param studentG4s 学生信息储存列表
     * @param minAge 最小年龄
     * @param maxAge 最大年龄
     * @return
     */
    public static LinkedList rangeAgeReturn(LinkedList<StudentG4> studentG4s, int minAge, int maxAge) {
        LinkedList<StudentG4> rangeAge = new LinkedList();
        for (StudentG4 member : studentG4s) {
            int age = member.getAge();
            if (age >= minAge && age <= maxAge) {
                rangeAge.add(member);
            }
        }
        if(rangeAge.size() == 0){
            System.out.println("没有找到在此区间范围的成员");
        }else{
            for (int i = 0; i < rangeAge.size(); i++) {
                System.out.println(rangeAge.get(i));
            }
        }
        return rangeAge;
    }


}
